﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using ZQNB.Common;
using ZQNB.Common.FileDbs;

namespace ZQNB.BaseLib.Core
{
    public class CoreDataHelper
    {
        private readonly IFileDbHelper _fileDbHelper;

        public CoreDataHelper(IFileDbHelper fileDbHelper)
        {
            _fileDbHelper = fileDbHelper;
        }
        public IList<T> Read<T>()
        {
            string filePath = MakeFilePath<T>();

            var instance = _fileDbHelper.Read<T>(filePath);
            return instance;
        }

        public void Save<T>(IList<T> list)
        {
            string filePath = MakeFilePath<T>();
            _fileDbHelper.Save(filePath, list);
        }

        //按照规则定义来生成文件的名字
        /// <summary>
        /// "App_Data\{0}.json"
        /// </summary>
        /// <typeparam name="T">{0}</typeparam>
        /// <returns>"App_Data\{0}.json"</returns>
        private string MakeFilePath<T>()
        {
            Type type = typeof(T);
            string filePath = MakeFilePath(type);
            return filePath;
        }

        private string MakeFilePath(Type type)
        {
            string className = type.Name;
            string fileName = string.Format("{0}\\{1}.json", "Core", className);
            string path = MyPathHelper.MakeAppDataFilePath(fileName);
            return path;
        }
    }
}
